from django.shortcuts import render
from .models import UserMessage

# Create your views here.
def loginform(request):
    if(request.method == 'GET'):
        return render(request,'login_form.html')
    elif(request.method=='POST'):
        name = request.POST.get('name','')
        password = request.POST.get('password','')
        print(name,password)
        # 判断算法存在，存在则校验密码，不存在增加到数据库
        user = UserMessage.objects.filter(name=name).first()
        if user:
            if user.password==password:
                print("登录成功")
            elif(user & user.password != password):
                print("密码错误")
        else:
            newUser = UserMessage()
            newUser.name = name
            newUser.password = password
            newUser.save()
    else:
        return "http method not supported!"

def index(request):
    return render(request, 'index.html')

def login(request):
    if(request.method == 'GET'):
        return render(request, 'login.html')
    elif(request.method=='POST'):
        pass
        name = request.POST.get('name','')
        password = request.POST.get('password','')


def getusers(request):
    user = None
    users = UserMessage.objects.all()
    if users:
        user = users[0]

    for u in users:
        print(u.name)

    return render(request,'user_message.html', {'my': user})
